//
//  HistoryViewController.m
//  ZingBrowser
//
//  Created by -Xh- on 3/4/13.
//  Copyright (c) 2013 -Xh-. All rights reserved.
//

#import "HistoryViewController.h"
#import "SGPageViewController.h"
@interface HistoryViewController ()

@end

@implementation HistoryViewController{
    
    HistoryGroupProvider* dataProvider;
    NSMutableArray* bookmarkArray;
    HistoryCell* currentLongPressingCell;
    NSIndexPath* currentLongPressingIndexPath;
    NSMutableArray* sectionHeaderStateList;
    UIImage* unknownImage;
    UIImage* dialerImage;
    UIImageView* tabBarOverlayIMV;
}


@synthesize tabBar;
@synthesize infoList;
@synthesize bookmarkItem;
@synthesize historyItem;
@synthesize defaultTabType;
@synthesize delegate;

NSString* kHeaderReuseIdentifier=@"headerReuse";
double kHeaderHeight=35;
double kRowHeight=65;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                              target:self
                                                                                              action:@selector(goBack)];
        
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
                                                                                             target:self
                                                                                             action:@selector(clearAll)];

        
        dataProvider=[[HistoryGroupProvider alloc]init];
        bookmarkArray=[[NSMutableArray alloc]init];
        sectionHeaderStateList=[[NSMutableArray alloc]init];
        unknownImage=[UIImage imageNamed:@"fav_icn_unknown.png"];
        dialerImage=[UIImage imageNamed:@"home2.png"];
        
        
        
        
    }
    return self;
}

#pragma controllerDelegate

-(void)reloadThemeForTabBar{
    //int currentThemeIndex= [[[[StaticVariables shareVariables]userDefaults]theme]intValue];
    //UIColor* currentThemeColor=[[[StaticVariables shareVariables]presetThemes]objectAtIndex:currentThemeIndex];
    
    //tabBar.tintColor=currentThemeColor;
    
    //if(currentThemeIndex==0){
    //    tabBar.tintColor=nil;
    //}
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UILongPressGestureRecognizer* longPressGR=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressOnCell:)];
    longPressGR.minimumPressDuration=[[[StaticVariables shareVariables]constantValues]longPressMinimumDuration];
    [self.infoList addGestureRecognizer:longPressGR];
    
    
    self.infoList.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    //[self.infoList registerNib:[UINib nibWithNibName:@"HistoryHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:kHeaderReuseIdentifier];
    //self.infoList.canbe
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated{
    
}
-(void)viewWillAppear:(BOOL)animated{
    [self prepareForTransition];
    if([self isAtBookmarkTab]){
        [self displayContentForTab:bookmarkItem];
    }else{
        [self displayContentForTab:historyItem];
        
    }
    
    //boolean array
    for(int i=0;i<dataProvider.groupDisplayNameArray.count;i++){
        BOOL iDefaultBool=NO;
        if(i==0){
            iDefaultBool=YES;
        }
        NSNumber* boolNumber=[NSNumber numberWithBool:iDefaultBool];
        [sectionHeaderStateList addObject:boolNumber];
    }
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self.infoList reloadData];
}

#pragma mark privateMethod    
-(void)clearAll{
    NSLog(@"clear now");
    
    [[[UIAlertView alloc]initWithTitle:[MyUtilities getLocalizeStringFromTag:@"AlertWarningTitle"]
                              message:[MyUtilities getLocalizeStringFromTag:@"AlertCheckSure"]
                             delegate:self
                     cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                    otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOK"], nil]show];
}
-(void)goBack{
    //self.navigationController.navigationBar.hidden=YES;
    //[self.navigationController popViewControllerAnimated:YES];
    if([self.browser isKindOfClass:[SGPageViewController class]]){
        [UIView animateWithDuration:[[[StaticVariables shareVariables]constantValues]vcFlipAnimationDuration]
                         animations:^{
                             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
                         }];
        self.navigationController.navigationBar.hidden=YES;
        [self.navigationController popViewControllerAnimated:NO];

    }else{
        if(delegate && [delegate respondsToSelector:@selector(onHistoryGoBack)]){
            [delegate onHistoryGoBack];
        }
    }
}
-(void)prepareForTransition{
    
    [self reloadThemeForTabBar];
    
    
    if(defaultTabType==TabHistory){
        [self.tabBar setSelectedItem:self.historyItem];
    }else{
        [self.tabBar setSelectedItem:self.bookmarkItem];

    }
    [[[StaticVariables shareVariables] editorView] autoAdjustToFrame:self.view.frame];
    [self.view addSubview:[[StaticVariables shareVariables]editorView]];
    [[StaticVariables shareVariables]editorView].delegate = self;
    
}
-(BOOL)isAtBookmarkTab{
    UITabBarItem* currentTab=[self.tabBar selectedItem];
    if([currentTab isEqual:bookmarkItem]){
        return YES;
    }else{
        return NO;
    }
}
-(void)displayContentForTab:(UITabBarItem*)_tabItem{
    
    if([_tabItem isEqual:bookmarkItem]){
        //display all Bookmark records
        bookmarkArray=[[[StaticVariables shareVariables] bookmarkAccess] getAllRecords];
        self.navigationItem.rightBarButtonItem.enabled=NO;
        
    }else{
        //display all History records
        
        [dataProvider update];
        self.navigationItem.rightBarButtonItem.enabled=YES;
        [dataProvider relocalize];
    }
    
    [infoList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
#pragma mark AlertViewDelegate
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    NSString* buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionOK"]]){
        //self.infoList.
        [[[StaticVariables shareVariables]historyAccess] deleteAll];
        [dataProvider update];
        [infoList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
    
}

#pragma mark uiTableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //if the provider still not load data, we display 0 row
    if([self isAtBookmarkTab]){
        return bookmarkArray.count;
    }else{
        
        NSMutableArray* dataListOnSection=[dataProvider.groupDataListArray objectAtIndex:section];
        BOOL sectionIsExpanding=[[sectionHeaderStateList objectAtIndex:section]boolValue];
        if(sectionIsExpanding){
            return dataListOnSection.count;
        }else{
            return 0;
        }
        
        
    }
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if([self isAtBookmarkTab]){
        return 1;
    }else{
        return dataProvider.groupDisplayNameArray.count;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"HistoryCellReuse";
    HistoryCell *cell;
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    HistoryModel* history;
    if([self isAtBookmarkTab]){
        history=[bookmarkArray objectAtIndex:indexPath.row];
    }else{
        NSMutableArray* dataListOnSection=[dataProvider.groupDataListArray objectAtIndex:indexPath.section];
        history=[dataListOnSection objectAtIndex:indexPath.row];
    }
    
    if (cell == nil) {
        cell=(HistoryCell*)[[[NSBundle mainBundle]loadNibNamed:@"HistoryCell" owner:self options:nil] objectAtIndex:0];
        [cell setUpStyle];
        cell.delegate=self;
        
    }
    
    SpeedDialModel* speedDialOnDB = [[[StaticVariables shareVariables]speedDialAccess]findByURL:history.url];
    BookmarkModel* bookmarkOnDB = [[[StaticVariables shareVariables]bookmarkAccess]findByURL:history.url];
    if(speedDialOnDB==nil){
        [cell.thumbnail setImage:unknownImage];
    }else{
        [cell.thumbnail setImage:dialerImage];

    }
    
    //save the currentHistory, store in cell
    cell.currentHistory=history;
    //get the star state from history and update the button
    if([self isAtBookmarkTab]){
        [cell.starButton swapToState:1];
    }else{
        [cell.starButton swapToState:bookmarkOnDB!=nil];
    }
    
    
    cell.titleLabel.text=history.title;
    cell.urlLabel.text=history.url;
    if([self isAtBookmarkTab]){
        cell.starButton.hidden=YES;
    }else{
        cell.starButton.hidden=NO;
    }
    
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self goBack];
    HistoryModel* history;
    if([self isAtBookmarkTab]){
        history=[bookmarkArray objectAtIndex:indexPath.row];
    }else{
        NSMutableArray* dataListOnSection=[dataProvider.groupDataListArray objectAtIndex:indexPath.section];
        history=[dataListOnSection objectAtIndex:indexPath.row];
    }
    
    if(self.delegate&&[self.delegate respondsToSelector:@selector(onUserClickOnCell:)]){
        [self.delegate onUserClickOnCell:history];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self isAtBookmarkTab]){
        return @"";
    }else{
        return [dataProvider.groupDisplayNameArray objectAtIndex:section];
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if([self isAtBookmarkTab]){
        return 0;
    }else{
        return kHeaderHeight;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if([self isAtBookmarkTab]){
        return nil;
    }else{
        HistoryHeader* headerView;
        BOOL sectionIsExpanding=[[sectionHeaderStateList objectAtIndex:section]boolValue];
        headerView=(HistoryHeader*)[[[NSBundle mainBundle]loadNibNamed:@"HistoryHeader" owner:self options:nil] objectAtIndex:0];
        [headerView initialize];
        headerView.delegate=self;
        headerView.title.text=[dataProvider.groupDisplayNameArray objectAtIndex:section];
        headerView.currentSectionIndex=section;
        [headerView.triggerButton swapToStateInBool:sectionIsExpanding];
        return headerView;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kRowHeight;
}
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(onLongPressCellSetAsHomePage))
        return YES;
    return NO;
}

#pragma mark historyHeaderDelegate

UITableViewRowAnimation rowAnimation=UITableViewRowAnimationMiddle;
-(void)onExpandHeader:(HistoryHeader *)_header{
    BOOL sectionIsExpanding=[[sectionHeaderStateList objectAtIndex:_header.currentSectionIndex]boolValue];
    NSNumber* newState=[NSNumber numberWithBool:!sectionIsExpanding];
    [sectionHeaderStateList replaceObjectAtIndex:_header.currentSectionIndex withObject:newState];
    
    NSMutableArray* dataOnSection=[dataProvider.groupDataListArray objectAtIndex:_header.currentSectionIndex];
    NSMutableArray* indexPathArray=[[NSMutableArray alloc]init];
    for(int i=0;i<dataOnSection.count;i++){
        NSIndexPath* iPath=[NSIndexPath indexPathForRow:i inSection:_header.currentSectionIndex];
        [indexPathArray addObject:iPath];
    }
    [self.infoList beginUpdates];
    [self.infoList insertRowsAtIndexPaths:indexPathArray withRowAnimation:rowAnimation];
    [self.infoList endUpdates];
    
    //[self.infoList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
-(void)onCollapseHeader:(HistoryHeader *)_header{
    BOOL sectionIsExpanding=[[sectionHeaderStateList objectAtIndex:_header.currentSectionIndex]boolValue];
    NSNumber* newState=[NSNumber numberWithBool:!sectionIsExpanding];
    [sectionHeaderStateList replaceObjectAtIndex:_header.currentSectionIndex withObject:newState];
    
    NSMutableArray* dataOnSection=[dataProvider.groupDataListArray objectAtIndex:_header.currentSectionIndex];
    NSMutableArray* indexPathArray=[[NSMutableArray alloc]init];
    for(int i=0;i<dataOnSection.count;i++){
        NSIndexPath* iPath=[NSIndexPath indexPathForRow:i inSection:_header.currentSectionIndex];
        [indexPathArray addObject:iPath];
    }
    [self.infoList beginUpdates];
    [self.infoList deleteRowsAtIndexPaths:indexPathArray withRowAnimation:rowAnimation];
    [self.infoList endUpdates];
}
#pragma mark longPressGesture

-(void)showActionSheetForCell:(HistoryCell*)_cell{
    UIActionSheet* actionSheet;
    SpeedDialModel* speedDialOnDB = [[[StaticVariables shareVariables]speedDialAccess]findByURL:_cell.currentHistory.url];

    if([self isAtBookmarkTab]){
        
        if(speedDialOnDB == nil){

            actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                   delegate:self
                                          cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionEdit"], [MyUtilities getLocalizeStringFromTag:@"ActionSetAsHomePage"],[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromBookmark"], nil];
            
        }else{
            actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                   delegate:self
                                          cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionEdit"],[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromSpeedDial"],[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromBookmark"],nil];
        }
        
    }else{
        BookmarkModel* bookmarkOnDB = [[[StaticVariables shareVariables]bookmarkAccess]findByURL:_cell.currentHistory.url];

        if(bookmarkOnDB == nil){
            if(speedDialOnDB==nil){
                actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                       delegate:self
                                              cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionSetAsHomePage"],[MyUtilities getLocalizeStringFromTag:@"ActionDelete"],nil];

            }else{
                actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                       delegate:self
                                              cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromSpeedDial"],nil];
            }
           
        }else{
            if(speedDialOnDB==nil){
                actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                       delegate:self
                                              cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionSetAsHomePage"],nil];
            }else{
                actionSheet=[[UIActionSheet alloc]initWithTitle:_cell.currentHistory.title
                                                       delegate:self
                                              cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"],[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"],[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromSpeedDial"],nil];
            }
            
        }
        
        
    }
    [actionSheet showInView:self.view];
}

-(void)handleLongPressOnCell:(UILongPressGestureRecognizer*)_longPressGR{
    
    if(_longPressGR.state==UIGestureRecognizerStateBegan){
        //[self.infoList becomeFirstResponder];
        //[self becomeFirstResponder];
        CGPoint pressLocation = [_longPressGR locationInView:self.infoList];
       currentLongPressingIndexPath= [self.infoList indexPathForRowAtPoint:pressLocation];
        /*
        if([self isAtBookmarkTab]){
            currentLongPressingHistory=[bookmarkArray objectAtIndex:pressedIndexPath.row];
        }else{
            NSMutableArray* dataListOnSection=[dataProvider.groupDataListArray objectAtIndex:pressedIndexPath.section];
            currentLongPressingHistory=[dataListOnSection objectAtIndex:pressedIndexPath.row];
        }*/
        currentLongPressingCell=(HistoryCell*)[self.infoList cellForRowAtIndexPath:currentLongPressingIndexPath];
        
        [self showActionSheetForCell:currentLongPressingCell];
        
    }
}
#pragma uiactionsheet
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString* buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionSetAsHomePage"]]){
        
    }
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    NSString* buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionOpen"]]){
        [self goBack];
        if(self.delegate&&[self.delegate respondsToSelector:@selector(onUserClickOnCell:)]){
            [self.delegate onUserClickOnCell:currentLongPressingCell.currentHistory];
        }
    }
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionOpenInNewTab"]]){
        [self goBack];
        if(self.delegate&& [self.delegate respondsToSelector:@selector(onOpenCellInNewTab:)]){
            [self.delegate onOpenCellInNewTab:currentLongPressingCell.currentHistory];
        }
    }
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionDelete"]]){
        [[[StaticVariables shareVariables]historyAccess] deleteObjectAtURL:currentLongPressingCell.currentHistory.url];
        NSMutableArray* dataListOnSection=[dataProvider.groupDataListArray objectAtIndex:currentLongPressingIndexPath.section];
        [dataListOnSection removeObject:currentLongPressingCell.currentHistory];
        [dataProvider updateSectionTitle];
        [self.infoList beginUpdates];
        [self.infoList deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentLongPressingIndexPath] withRowAnimation:rowAnimation];
        [self.infoList endUpdates];
        //[self.infoList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        
    }
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromBookmark"]]){
        [self removeBookmarkFromDBWithHistory:currentLongPressingCell.currentHistory];
        [self.infoList beginUpdates];
        [self.infoList deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentLongPressingIndexPath] withRowAnimation:rowAnimation];
        [self.infoList endUpdates];
        //[self.infoList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionRemoveFromSpeedDial"]]){


        [[[StaticVariables shareVariables]speedDialAccess] deleteObjectAtURL:currentLongPressingCell.currentHistory.url];
        [self.infoList beginUpdates];
        [self.infoList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:currentLongPressingIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
        [self.infoList endUpdates];
        if(delegate&&[delegate respondsToSelector:@selector(onChooseRemoveSpeedDialFromHistoryAtID:)]){
            [delegate onChooseRemoveSpeedDialFromHistoryAtID:currentLongPressingCell.currentHistory.historyID];
        }
    }
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionSetAsHomePage"]]){


        
        SpeedDialModel* speedDial=[[SpeedDialModel alloc]initWithTitle:currentLongPressingCell.currentHistory.title AndURL:currentLongPressingCell.currentHistory.url];
        [[[StaticVariables shareVariables]speedDialAccess]insert:speedDial];
        [self.infoList beginUpdates];
        [self.infoList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:currentLongPressingIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
        [self.infoList endUpdates];
        if(delegate&& [delegate respondsToSelector:@selector(onChooseSetAsSpeedDialWithModel:)]){
            [delegate onChooseSetAsSpeedDialWithModel:speedDial];
        }
    }
    
    if([buttonTitle isEqualToString:[MyUtilities getLocalizeStringFromTag:@"ActionEdit"]]){
        [self.view bringSubviewToFront:[[StaticVariables shareVariables] editorView]];
        [[[StaticVariables shareVariables] editorView] show];
        [[[StaticVariables shareVariables] editorView] receiveModel:currentLongPressingCell.currentHistory WithType:UpdateBookmark];
        
    }


}


#pragma mark HistoryCellDelegate
-(void)saveBookmarkToDBWithHistory:(HistoryModel *)_currentHistory{

    [[[StaticVariables shareVariables] bookmarkAccess] insertFromHistory:_currentHistory];
}
-(void)removeBookmarkFromDBWithHistory:(HistoryModel *)_currentHistory{
    //update history, isBookmark set to 0

    [bookmarkArray removeObject:_currentHistory];
    //delete from bookmark
    [[[StaticVariables shareVariables]bookmarkAccess] deleteObjectAtURL:_currentHistory.url];
}
#pragma mark UITableViewDelegate

#pragma mark UITabBarDelegate
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    [self displayContentForTab:item];
}


#pragma mark EditorDelegate
-(void)onEditorUpdatingBookmarkSuccessfully:(BookmarkModel *)_bookmark{
    [[[StaticVariables shareVariables]editorView] hide];
    [self.infoList beginUpdates];
    [self.infoList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:currentLongPressingIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
    [self.infoList endUpdates];
    
}
-(void)onEditorCancel{
    [[[StaticVariables shareVariables]editorView] hide];
}

@end
